﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UscSunShine.Models;

namespace UscSunShine.ViewModels.ExtensionMethod
{
    public static class PostExtension
    {
        /// <summary>
        /// Post转换为ViewModel
        /// </summary>
        /// <param name="model">公告</param>
        /// <returns></returns>
        public static PostViewModel ToView(this Post model)
        {
            var view = new PostViewModel
            {
                Content = model.Content,
                Department = model.Department,
                DepartmentID = model.DepartmentID,
                DepartmentName = model.Department.Name,
                ID = model.ID,
                InputDate = model.InputDate,
                PostCategory = model.PostCategory,
                PostCategoryName = EnumExtension.GetEnumCustomDescription(model.PostCategory),
                Title = model.Title,
                ViewTimes = model.ViewTimes
            };
            return view;
        }

        /// <summary>
        /// post列表转换为ViewModel列表
        /// </summary>
        /// <param name="models">公告列表</param>
        /// <returns></returns>
        public static IEnumerable<PostViewModel> ToViews(this IEnumerable<Post> models)
        {
            var views = new List<PostViewModel>();
            foreach (var item in models)
            {
                var view = item.ToView();
                views.Add(view);
            }
            return views;
        }

        /// <summary>
        /// PostViewModel转换为Post
        /// </summary>
        /// <param name="view">公告ViewModel</param>
        /// <returns></returns>
        public static Post ToModel(this PostViewModel view)
        {
            var model = new Post
            {
                Content = view.Content,
                Department = view.Department,
                DepartmentID = view.DepartmentID,
                ID = view.ID,
                InputDate = view.InputDate,
                PostCategory = view.PostCategory,
                Title = view.Title,
                ViewTimes = view.ViewTimes
            };
            return model;
        }

        /// <summary>
        /// PostViewModel列表转换为Post列表
        /// </summary>
        /// <param name="views">PostViewModel列表</param>
        /// <returns></returns>
        public static IEnumerable<Post> ToModels(this IEnumerable<PostViewModel> views)
        {
            var models = new List<Post>();
            foreach (var item in views)
            {
                var model = item.ToModel();
                models.Add(model);
            }
            return models;
        }
    }
}
